package com.hepinghospital.user.ui.activity.nursing;


import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.NurseListResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.DpUtil;
import jz.joyoung.robot.util.SoftKeyboardUtil;
import jz.joyoung.robot.util.UIConstants;
import jz.joyoung.robot.util.UtilString;

public class AppointNurseListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView doctor_recyclerView;
    @BindView(R.id.edt_search)
    EditText edt_search;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    List<NurseListResponse.RecordsBean> doctorList = new ArrayList<>();
    DoctorAdapter doctorAdapter;
    int current = 1;
    String searchContent = "", deptId = "", code = "", isPrescription = "", deptName = "", checkDetailId;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_appoint_nurse_list;
    }

    @Override
    protected void initViews() {
        tvTitle.setText("护理预约");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        doctor_recyclerView.setLayoutManager(manager);
        doctorAdapter = new DoctorAdapter();
        doctor_recyclerView.setAdapter(doctorAdapter);
        doctor_recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(AppointNurseListActivity.this, 16);
            }
        });

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            current = 1;
            getNurseList(searchContent);
        });
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            current++;
            getNurseList(searchContent);
        });
        edt_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 按下完成按钮，这里和上面imeOptions对应
                searchContent = edt_search.getText().toString();
                current = 1;
                getNurseList(searchContent);
                SoftKeyboardUtil.hideSoftKeyboard(this);
            }
            return false;
        });
    }

    @Override
    protected void initData() {

        if (!UtilString.isEmpty(deptName)) {
            //从推荐科室过来的,选中科室
            deptId = getIntent().getStringExtra("deptId");
        }
        getNurseList(searchContent);
    }

    /**
     * 医生列表
     *
     * @time 2020/5/11 16:00
     */
    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @Override
        public DoctorAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            DoctorAdapter.Holder myHolder;
            myHolder = new DoctorAdapter.Holder(LayoutInflater.from(AppointNurseListActivity.this).inflate(R.layout.nurse_list_item, parent, false));
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(DoctorAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> startAppointNursePayActivity(position));
            holder.tv_skill.setText("简介:" + doctorList.get(position).getIntroduction());
            holder.tv_level.setText(doctorList.get(position).getJobTitle());
            holder.tv_star.setText(TextUtils.isEmpty(doctorList.get(position).getScore()) ? "0.0" : doctorList.get(position).getScore());
            holder.tv_name.setText(doctorList.get(position).getName());
            ImageLoaderHelper.getInstance().GlideCircleImageLoader(AppointNurseListActivity.this, doctorList.get(position).getDoctorPicture(), holder.img_head);
            holder.tv_turnNum.setText("咨询数 " + doctorList.get(position).getConsultCount());
            holder.tv_deptName.setText(doctorList.get(position).getDeptName());
            holder.tv_star.setText(TextUtils.isEmpty(doctorList.get(position).getScore()) ? "0.0" : doctorList.get(position).getScore());
            if ("0".equals(doctorList.get(position).isRecommend())) {
                holder.ivRecommend.setVisibility(View.GONE);
            } else {
                holder.ivRecommend.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_level, tv_recipel, tv_deptName, tv_skill, tv_star, tv_turnNum, ivRecommend;
            ImageView img_head;
            RatingBar ratingBar;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_recipel = itemView.findViewById(R.id.tv_recipel);
                tv_deptName = itemView.findViewById(R.id.tv_deptName);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_star = itemView.findViewById(R.id.tv_star);
                img_head = itemView.findViewById(R.id.img_head);
                ratingBar = itemView.findViewById(R.id.ratingBar);
                tv_turnNum = itemView.findViewById(R.id.tv_turn_num);
                ivRecommend = itemView.findViewById(R.id.iv_recommend);
            }
        }
    }

    public void getNurseList(String content) {
        RestProxy.getInstance().getNurseList(content, current + "", "10", new Observer<NurseListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NurseListResponse nurseListResponse) {
                if (current == 1) {
                    if (nurseListResponse.getRecords() != null) {
                        doctorList.clear();
                        doctorList.addAll(nurseListResponse.getRecords());
                        refreshLayout.finishRefresh(true);
                    }
                } else {
                    if (nurseListResponse.getRecords() != null && nurseListResponse.getRecords().size() > 0) {
                        doctorList.addAll(nurseListResponse.getRecords());
                        refreshLayout.finishLoadMore();
                    } else {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
                doctorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(AppointNurseListActivity.this, e);
                if (current == 1) {
                    refreshLayout.finishRefresh(true);
                } else {
                    refreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void startAppointNursePayActivity(final int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIConstants.APPOINT_NURSE_PAY_PARAMS, doctorList.get(position));
        startActivity(AppointNursePayActivity.class, bundle);
    }

}